//
//  FCSelectLanguageVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/3/5.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCSelectLanguageVC.h"
#import "FCSelectLanguageCell.h"
@interface FCSelectLanguageVC ()<UITableViewDelegate,UITableViewDataSource>{
    UIView     *_viewNav;
}
/* tableView */
@property (strong, nonatomic) UITableView *tableView;
/* 记录上一次点击的cell */
@property (strong, nonatomic) NSIndexPath *lastSelect;
/* 数据源 */
@property (strong, nonatomic) NSMutableArray *tbvDotaSource;
@end

@implementation FCSelectLanguageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [FCLocalUserHelper saveIsMainVC:@"0"];
    //初始化数据
    NSArray *array = @[@{@"voice":@"简体中文",@"code":@"zh-Hans"},@{@"voice":@"English",@"code":@"en"},@{@"voice":@"繁體中文",@"code":@"zh-Hant-TW"}];
    self.tbvDotaSource = [NSMutableArray arrayWithArray:array];
   [self setNav];
    [self setupUI];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(self.lastSelect)
    {
        UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:self.lastSelect];
        UIButton *but = (UIButton*)lastCell.accessoryView;
        but.selected = NO;
    }
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *but = (UIButton*)cell.accessoryView;
    but.selected = !but.selected;
    self.lastSelect = indexPath;
    switch (indexPath.row) {
        case 0:
            {
            [CLanguageUtil setCurrentLanguage:Language_Hans];
             
            }
            break;
        case 1:
        {
            [CLanguageUtil setCurrentLanguage:Language_EN];
        }
            break;
        case 2:
        {
            [CLanguageUtil setCurrentLanguage:Language_Hant];
        }
            break;
        default:
            break;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [FCLocalUserHelper pushMainTabbarController];
    });
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120*KScreenHeightScale1334;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"languageCell"];
    cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"languageCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary * dict = self.tbvDotaSource[indexPath.row];
    NSString* strLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:@"userLanguage"];
    NSLog(@"%@", strLanguage);
    
    
    cell.textLabel.text = [dict objectForKey:@"voice"];
    UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    if([[dict objectForKey:@"code"] isEqualToString:strLanguage]){
        but.selected = YES;
        self.lastSelect = indexPath;
    }
    [but setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [but setImage:[UIImage imageNamed:@"chosen"] forState:UIControlStateSelected];
    [but addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    but.userInteractionEnabled = false;
    cell.accessoryView = but;
//    Language language = [CLanguageUtil getCurrentLanguage];
    UIView *viewLine =[UIView new];
    [cell addSubview:viewLine];
    [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    viewLine.backgroundColor =kLineColor;

    
    
    return cell;
}
-(void)click:(UIButton *)sender{
    UITableViewCell *senderCell = (UITableViewCell *)sender.superview.superview;
    [self tableView:self.tableView didSelectRowAtIndexPath:[self.tableView indexPathForCell:senderCell]];
}
-(void)setupUI{
    
    [self tableView];
    
    
}
//懒加载tableView
-(UITableView*)tableView{
    
    if(!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView ];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_viewNav.mas_bottom);
            make.left.mas_offset(0);
            make.right.mas_offset(0);
            make.bottom.mas_offset(0);
        }];
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = kWhiteColor;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
#pragma mark-设置导航栏
-(void)setNav{
    self.navigationController.navigationBarHidden = YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(88);
            }else{
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(64);
                make.top.mas_equalTo(0);
            }
        }else{
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(64);
            make.top.mas_equalTo(0);
        }
        
    }];
    _viewNav.backgroundColor = kRedOrangeColor;
    
    UIButton *leftBtn = [UIButton new];
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*KScreenWidthScale750);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
       
    }];
    [leftBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //返回
    UILabel *backLabel = [UILabel new];
    
    [_viewNav addSubview:backLabel];
    [backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_viewNav);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
    }];
    backLabel.text = Localized(@"语言");
    backLabel.textColor = [UIColor whiteColor];
    backLabel.font = KOverstrikingfont(36);
    
    //事件
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)leftBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
